home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magnum One
/
Magnum One (Mid-American Digital) (Disc Manufacturing).iso
/
d12
/
ddj_cspc.arc
/
STEVENS.LST
< prev
next >
Wrap
File List
|
1989-12-13
|
3KB
|
192 lines
_C PROGRAMMER'S GUIDE TO C++_
by Al Stevens
Example 1: Global variables
int amount = 123;
main()
{
int amount = 456;
printf("%d", ::amount);
printf("%d", amount);;
}
Example 2: Structures with functions
struct cube {
int length;
int width;
int height;
int volume();
};
Example 3: A class definition
class date {
int day;
int month;
int year;
public:
date(void)
{day = month = year = 0;}
date(int da, int mo, int yr);
~date() { /* null destructor */ }
void display(void);
};
Example 4: Constructor function
date::date(int da, int mo, int yr)
{
day = da;
month = mo;
year = yr;
}
Example 5: Making an assignment of friend status
class time;
class date {
// ...
friend void now(date&, time&);
};
class time {
// ...
friend void shownow(date&, time&);
};
Example 6: A simple date class
date retirement_date, today;
// ...
if (retirement_date < today)
// Keep working ...
How about this?
date date_married, today;
// ...
if (date_married + 365 == today)
// Happy Anniversary ...
Example 7: A member function
int date::operator<(date& dt)
{
if (year < dt.year)
return TRUE;
if (year == dt.year) {
if (month < dt.month)
return TRUE;
if (month == dt.month)
if (day < dt.day)
return TRUE;
}
return FALSE;
}
Example 8: An overloaded function
long date::operator long()(void)
{
long days_since_creation;
// compute the number of days ...
// ...
return days_since_creation;
}
Example 9: One way of calling the function listed in Example 8
void main()
{
date today;
long eversince;
// ...
eversince = (long)today;
eversince = today;
eversince = long(today);
}
Example 10: Member functions
class workday : date {
public:
// ...
int isXmas(void);
};
int workday::isXmas(void)
{
return month == 12 && day == 25;
}
Example 11: Non-member functions
class workday : public date {
// ...
};
void main()
{
workday proj_dt;
// ...
int yr_comp = proj_dt.year;
}
Example 12: Inheriting attributes of multiple base classes
class date {
// ...
};
class time {
// ...
};
class datetime : date, time {
// ...
public:
datetime(int da,int mo,int yr,
int hr,int min, int sec);
};
Example 13: Examples of C++ virtual functions
class date {
// ...
public:
virtual int isXmas(void);
};
class workday : date {
public:
int isXmas(void);
};
void main()
{
date dt;
workday wd;
date *dat = &dt;
// ...
wd.isXmas(); // workday::isXmas
dt.isXmas(); // date::isXmas
dat->isXmas(); // workday::isXmas (!)
wd.date::isXmas(); // date::isXmas
}